package com.thb.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.entity.AgentpayPassage;
import com.thb.pay.mapper.AgentpayPassageMapper;
import com.thb.pay.service.AgentpayPassageService;
import com.thb.pay.vo.req.AgentpayPassagePageReq;
import com.thb.pay.vo.req.AgentpayPassageReq;
import com.thb.pay.vo.resp.AgentpayPassageVo;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 代付-通道表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Service
public class AgentpayPassageServiceImpl extends BaseServiceImpl<AgentpayPassageMapper, AgentpayPassage> implements AgentpayPassageService {

    @Override
    public PageResult<AgentpayPassageVo> findPage(AgentpayPassagePageReq pageReq) {
        IPage<AgentpayPassage> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<AgentpayPassage> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, AgentpayPassageVo.class);
    }

    @Override
    public void add(AgentpayPassageReq agentpayPassageReq) {
        Assert.notNull(agentpayPassageReq, "agentpayPassageReq.not.null");
        AgentpayPassage agentpayPassage = new AgentpayPassage();
        BeanUtil.copyProperties(agentpayPassageReq, agentpayPassage);
        super.insert(agentpayPassage);
    }

    @Override
    public void update(AgentpayPassageReq agentpayPassageReq) {
        Assert.notNull(agentpayPassageReq, "agentpayPassageAccountReq.not.null");
        Assert.notNull(agentpayPassageReq.getId(), "agentpayPassageId.not.null");
        AgentpayPassage agentpayPassage = super.selectById(agentpayPassageReq.getId());
        Assert.notNull(agentpayPassage, "agentpayPassage.not.null");
        AgentpayPassage targetAgentpayPassage = new AgentpayPassage();
        BeanUtil.copyProperties(agentpayPassageReq, targetAgentpayPassage);
        super.updateById(targetAgentpayPassage);
    }

    @Override
    public AgentpayPassageVo getById(Integer id) {
        Assert.notNull(id, "agentpayPassageId.not.null");
        AgentpayPassage agentpayPassage = super.selectById(id);
        if (agentpayPassage != null) {
            AgentpayPassageVo agentpayPassageVo = new AgentpayPassageVo();
            BeanUtil.copyProperties(agentpayPassage, agentpayPassageVo);
            return agentpayPassageVo;
        }
        return null;
    }

    @Override
    public List<AgentpayPassageVo> findAllList(AgentpayPassageReq conditionParam) {
        List<AgentpayPassageVo> targetResultList = Lists.newArrayList();
        LambdaQueryWrapper<AgentpayPassage> queryWrapper = new LambdaQueryWrapper<>();
        if (conditionParam != null) {
            if (conditionParam.getIsDefault() != null) {
                queryWrapper.eq(AgentpayPassage::getIsDefault, conditionParam.getIsDefault());
            }
            if (conditionParam.getRiskStatus() != null && conditionParam.getRiskStatus() != -99) {
                queryWrapper.eq(AgentpayPassage::getRiskStatus, conditionParam.getRiskStatus());
            }
            if (conditionParam.getStatus() != null && conditionParam.getStatus() != -99) {
                queryWrapper.eq(AgentpayPassage::getStatus, conditionParam.getStatus());
            }
        }
        queryWrapper.orderByDesc(AgentpayPassage::getCreateTime);
        List<AgentpayPassage> resResultList = super.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(resResultList)) {
            resResultList.forEach(agentpayPassage -> {
                AgentpayPassageVo agentpayPassageVo = new AgentpayPassageVo();
                BeanUtil.copyProperties(agentpayPassage, agentpayPassageVo);
                targetResultList.add(agentpayPassageVo);
            });
        }
        return targetResultList;
    }
}
